package com.graduate.controller;

import com.graduate.exception.BizException;
import com.graduate.exception.ExceptionEnum;
import com.graduate.utils.BaseServiceUtils;
import com.graduate.service.ArticleService;
import com.graduate.service.PictureService;
import com.graduate.utils.RespBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.graduate.utils.ConstantConfig.ARTICLE_TYPE_NEWS;
import static com.graduate.utils.ConstantConfig.ARTICLE_TYPE_PUBLIC;

/**
 * 获得首页数据
 *
 * @Createtime : 2020-04-01
 * @Creator : jingchu
 */
@RestController
//@Controller(配合ResponseBody使用)
@EnableAutoConfiguration
public class HomePageController extends BaseServiceUtils {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private PictureService pictureService;

    @Retryable(value = BizException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    @RequestMapping(value = "/getHomePage", method = RequestMethod.GET)
    @Transactional
    public RespBody getHomePage(@RequestParam("associator") String associator, @RequestParam("openid") String openid) {
        Map<String, Object> map = new HashMap<>();
        map.put("associator", associator);
        map.put("openid", openid);
        map.put("op_type", "A02");
        map.put("op_desc", "查看首页");
        insertRecords(map);
        List<Map<String, Object>> publicList = articleService.getArticleList("0", "6", "0");
        List<Map<String, Object>> bannerList = pictureService.getBanner("6", "0");
        Map<String,Object> responseMap = new HashMap<>();
        responseMap.put("public",publicList);
        responseMap.put("banner",bannerList);
        return RespBody.success(responseMap);
    }

    @Retryable(value = BizException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    @RequestMapping(value = "/getSchoolArticleList", method = RequestMethod.GET)
    @Transactional
    public RespBody getSchArticleList(@RequestParam("id") String id, @RequestParam("openid") String openid, @RequestParam("limit") String limit, @RequestParam("offset") String offset, @RequestParam("type") String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("associator", id);
        map.put("openid", openid);
        if (type.equals(ARTICLE_TYPE_NEWS)) {
            map.put("op_type", "A03");
            map.put("op_desc", "查看学校新闻列表");
        } else if (type.equals(ARTICLE_TYPE_PUBLIC)) {
            map.put("op_type", "A04");
            map.put("op_desc", "查看学校公告列表");
        } else {
            logger.error("参数不符合标准");
            throw new BizException(ExceptionEnum.BODY_NOT_MATCH);
        }
        insertRecords(map);
        List<Map<String, Object>> articleList = articleService.getArticleList(type, limit, offset);
        return RespBody.success(articleList);
    }
}

